package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealDao setmealDao;

    @Override
    public void add(Setmeal setmeal) {
        //保存套餐基本信息并返回主键
        setmealDao.add(setmeal);
        //建立套餐和检查组的关系
        Integer setmealId = setmeal.getId();
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
        if(CollectionUtil.isNotEmpty(checkgroupIds)){
//            for (Integer checkgroupId : checkgroupIds) {
//                setmealDao.setSetmealAndCheckGroup(setmealId,checkgroupId);
//            }

            List<Map> batchMap = new ArrayList<>();
            for (Integer checkgroupId : checkgroupIds) {
                Map map = new HashMap();
                map.put("checkgroupId",checkgroupId);
                map.put("setmealId",setmealId);
                batchMap.add(map);
            }
            setmealDao.setSetmealAndCheckGroupBatch(batchMap);

        }

    }
}
